﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using kwzx.Data;

namespace kwzx.kq
{
    public partial class frmAttendance : Form
    {
        public frmAttendance()
        {
            InitializeComponent();
        }


        DataSet1TableAdapters.K_DeviceIssueTableAdapter k_deviceIssue = new DataSet1TableAdapters.K_DeviceIssueTableAdapter();
        DataSet1TableAdapters.T_BMTableAdapter bm = new DataSet1TableAdapters.T_BMTableAdapter();
        DataSet1TableAdapters.T_GROUPTableAdapter group = new DataSet1TableAdapters.T_GROUPTableAdapter();
        DataSet1TableAdapters.CD_CardTypeTableAdapter cd_card = new DataSet1TableAdapters.CD_CardTypeTableAdapter();
        DataSet1TableAdapters.K_CardTableAdapter k_card = new DataSet1TableAdapters.K_CardTableAdapter();


        private void fk_Load(object sender, EventArgs e)
        {
            this.radioButton1.Checked = true;

            DataTable group_tb = group.GetDataBy_sjjg(0);
            for (int g = 0; g < group_tb.Rows.Count; g++)
            {
                TreeNode tn = new TreeNode();
                tn = treeView1.Nodes.Add(group_tb.Rows[g]["JGNAME"].ToString());
                tn.Text = group_tb.Rows[g]["jc"].ToString();
                tn.Tag = -1;
                tn.ToolTipText = "jg";
                tn.ImageIndex = 0;
                tn.SelectedImageIndex = 0;
                addBMTree(group_tb.Rows[g]["GRPUP_ID"].ToString(), "0", tn);
            }
            //外协单位
            TreeNode tn1 = new TreeNode();
            tn1 = treeView1.Nodes.Add("外协单位");
            tn1.Text = "外协单位";
            tn1.Tag = "0";
            tn1.ToolTipText = "jg";
            tn1.ImageIndex = 0;
            tn1.SelectedImageIndex = 0;
            tn1.Checked = true;
            addJGTree(group_tb.Rows[0]["GRPUP_ID"].ToString(), tn1);
            treeView1.Nodes[0].ExpandAll();
            treeView1.SelectedNode = treeView1.Nodes[0].NextVisibleNode;

        }

        /// <summary>
        /// 递归加载addJGTree列表
        /// </summary>
        /// <param name="fatherid">节点的fatherid</param>
        /// <param name="pnode">该节点</param>
        private void addJGTree(string fatherid, TreeNode pnode)
        {
            DataTable dt = group.GetDataBy_sjjg(int.Parse(fatherid));
            DataView dvTree = new DataView(dt);
            dvTree.RowFilter = "[SJJG] = " + fatherid;
            foreach (DataRowView drv in dvTree)
            {
                TreeNode node = new TreeNode();
                node.ImageIndex = 1;
                node.SelectedImageIndex = 3;
                if (pnode == null)
                {
                    node.Text = drv["jc"].ToString();
                    node.Tag = drv["GRPUP_ID"].ToString();    //作为节点标识符
                    node.ToolTipText = "jg";
                    treeView1.Nodes.Add(node);
                    addJGTree(drv["GRPUP_ID"].ToString(), node);//递归  该节点加载子节点
                }
                else
                {
                    node.Text = drv["jc"].ToString();
                    node.Tag = drv["GRPUP_ID"].ToString();//作为节点标识符
                    node.ToolTipText = "jg";
                    pnode.Nodes.Add(node);
                    addJGTree(drv["GRPUP_ID"].ToString(), node);//递归  该节点加载子节点
                }
            }
        }

        /// <summary>
        /// 递归加载addBMTree列表
        /// </summary>
        /// <param name="fatherid">节点的fatherid</param>
        /// <param name="pnode">该节点</param>
        private void addBMTree(string jgid, string fatherid, TreeNode pnode)
        {
            DataTable dt = bm.GetDataBy_grpup_id(int.Parse(jgid));
            //  DataTable dt = bm.GetDataBy_oderBMMC(int.Parse(jgid));
            DataView dvTree = new DataView(dt);
            dvTree.RowFilter = "[SJBM] = " + fatherid;
            foreach (DataRowView drv in dvTree)
            {
                TreeNode node = new TreeNode();
                node.ImageIndex = 1;
                node.SelectedImageIndex = 3;
                if (pnode == null)
                {
                    node.Text = drv["BMMC"].ToString();
                    node.Tag = drv["DEPT_ID"].ToString();    //作为节点标识符
                    node.ToolTipText = "bm";
                    treeView1.Nodes.Add(node);
                    addBMTree(jgid, drv["DEPT_ID"].ToString(), node);//递归  该节点加载子节点
                }
                else
                {
                    node.Text = drv["BMMC"].ToString();
                    node.Tag = drv["DEPT_ID"].ToString();//作为节点标识符
                    node.ToolTipText = "bm";
                    pnode.Nodes.Add(node);
                    addBMTree(jgid, drv["DEPT_ID"].ToString(), node);//递归  该节点加载子节点
                }
            }
        }



        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            Data.HXYKTEntities db = new Data.HXYKTEntities();
            Data.HWATT1Entities hwatt = new Data.HWATT1Entities();
            var listhw = hwatt.KQZ_Employee.Where(a => a.BrchID != -3);
            if (radio1.Checked)
            {

                foreach (var item in listhw)
                {
                    string rybh = item.EmployeeCode;
                    var hxykt = db.View_UserInfo.Where(a => a.rybh==(rybh)).ToArray();
                    if (hxykt.Length == 1)
                    {
                        var model = hwatt.KQZ_Employee.Where(a => a.EmployeeID == item.EmployeeID).FirstOrDefault();
                        model.EmployeeName = hxykt[0].XM;
                        model.IDCard = hxykt[0].sfzh;
                        string deptName = hxykt[0].BMMC;
                        var brchModel = hwatt.KQZ_Brch.Where(a => a.BrchName == deptName).FirstOrDefault();
                        model.BrchID = brchModel.BrchID;
                        model.Reserved1 = "1";
                        model.Reserved2 = hxykt[0].rybh;
                        model.WorkPhone = hxykt[0].DH2;
                        model.Gender = (short)(hxykt[0].XB == "男" ? 1 : 0);

                    }
                    else
                    {
                        kwzx.Common.Log.WriteInfo(item.EmployeeCode + "该人员有" + hxykt.Length + "个");
                    }
                }
                hwatt.SaveChanges();
            }
            else
            {

                foreach (var item in listhw)
                {
                    string rybh = "0"+item.EmployeeCode;
                    var hxykt = db.View_UserInfo.Where(a => a.rybh.Contains(rybh)).ToArray();
                    if (hxykt.Length ==1)
                    {
                        var model = hwatt.KQZ_Employee.Where(a => a.EmployeeID == item.EmployeeID).FirstOrDefault();
                        model.EmployeeName = hxykt[0].XM;
                        model.IDCard = hxykt[0].sfzh;
                        string deptName = hxykt[0].BMMC;
                        var brchModel = hwatt.KQZ_Brch.Where(a => a.BrchName == deptName).FirstOrDefault();
                        model.BrchID = brchModel.BrchID;
                        model.Reserved1 = "1";
                        model.Reserved2 = hxykt[0].rybh;
                        model.WorkPhone = hxykt[0].DH2;
                        model.Gender =(short) (hxykt[0].XB == "男" ? 1 : 0) ;
               
                    }
                    else
                    {
                        kwzx.Common.Log.WriteInfo(item.EmployeeCode + "该人员有"+hxykt.Length+"个");
                    }
                }
                hwatt.SaveChanges();
            }


        }

        private void button2_Click(object sender, EventArgs e)
        {
            Data.HXYKTEntities db = new Data.HXYKTEntities();
            Data.HWATT1Entities hwatt = new Data.HWATT1Entities();
            var modle = db.T_BM.Where(a => a.GRPUP_ID != 83).ToArray();
            int max = int.Parse(hwatt.KQZ_Brch.Where(a => a.PID == 2).Max(a => a.BrchOrder).ToString());
            int brachid = int.Parse(hwatt.KQZ_Brch.Max(a => a.BrchID).ToString());
            int order = 4;
            foreach (var item in modle)
            {
                max++; order++; brachid++;
                KQZ_Brch brach = new KQZ_Brch();
                brach.BrchID = brachid;
                brach.PID = 2;
                brach.BrchOrder = short.Parse(max.ToString());
                brach.BrchName = item.BMMC;
                brach.ATTENDDAY = "1111100";
                brach.AMBEGIN = DateTime.Now;
                brach.AMEND = DateTime.Now;
                brach.PMBEGIN = DateTime.Now;
                brach.PMEND = DateTime.Now;
                brach.PARENTBRCHID = string.Format("0@2@{0}@", order);
                hwatt.AddToKQZ_Brch(brach);
                hwatt.SaveChanges();

            }
        }
    }
}
